//
//  BConst.h
//  Bullet
//
//  Created by  on 2019/9/18.
//  Copyright © 2019 . All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <CoreGraphics/CGBase.h>



NS_ASSUME_NONNULL_BEGIN

#ifdef DEBUG
#define BLog(fmt, ...) NSLog((@"VideoCacheAdd:VideoCache:%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define BLog(...)
#endif

// 服务器成功回调
typedef void(^ServiceSuccessBlock)(id result);
// 服务器失败回调
typedef void (^ServiceFailedBlock)(NSString *errorMs, NSInteger statusCode);
// 空回调
typedef void (^NullReturnBlock)(void);
// 空回调
typedef void (^UploadVideoProgress)(CGFloat progress,int64_t currentSendBytes);

// 视频未下载完地址数据缓存key
extern NSString * const VIDEO_LOADING_CACHE_MAP_KEY;
// 视频地址数据缓存key
extern NSString * const VIDEO_LOCAL_CACHE_MAP_KEY;
extern NSString * const VIDEO_FINISHED_CACHE_MAP_KEY;
// 视频缓存时间转字符串格式
extern NSString * const VIDEO_CACHE_TIME_FORMATE_KEY;
// 启动广告用户偏好
extern NSString * const ADVERTISE_USERDEFAULT_KEY;
// 登录用户信息偏好
extern NSString * const LOGIN_USERDEFAULT_KEY;
// 视频上传缓存
extern NSString * const UPLOAD_USERDEFAULT_KEY;

struct BVideoCacheResult{
    NSString *path;// 视频路径，web路径/缓存路径
    BOOL isLocalPath;// 是否为缓存路径
    CGFloat totalDuration;// 是否为缓存路径
};
typedef struct BVideoCacheResult BVideoCacheResult;

struct BRGB{
    CGFloat r;
    CGFloat g;
    CGFloat b;
    CGFloat a;
};
typedef struct BRGB BRGB;

struct BM3u8ContentFileExistResult{
    NSString *subP;// 文件夹路径
    BOOL isAllExist;// 是否所有包含文件都存在
};
typedef struct BM3u8ContentFileExistResult BM3u8ContentFileExistResult;

struct BVideoRecordConfig{
    BOOL isRecordVideo;// YES:录制视频 NO:拍照
    BOOL isFrontDevice;// YES:前置摄像头  NO:后置摄像头
};
typedef struct BVideoRecordConfig BVideoRecordConfig;


struct BButtonConfig{
    NSString *nStr;// 正常模式标题
    NSString *sStr;// 选中状态标题
    UIColor *nColor;// 正常标题颜色
    UIColor *sColor;// 选中标题颜色
    UIFont *titleFont;// 字体
    NSString *nImgStr;// 正常模式图片
    NSString *sImgStr;// 选中模式图片
    id target;// 事件链接
    SEL selector;// 点击方法
};
typedef struct BButtonConfig BButtonConfig;

CG_INLINE UIColor * BColorMake(CGFloat r,CGFloat g,CGFloat b,CGFloat a);
CG_INLINE BOOL IS_NULL_STR(NSString *str);
CG_INLINE NSString * ParseToString(id data);
CG_INLINE BRGB BRGBMake(CGFloat r,CGFloat g,CGFloat b,CGFloat a);
CG_INLINE BVideoCacheResult BVideoCacheResultMake(NSString *path,BOOL isLocalPath,CGFloat totalDuration);
CG_INLINE BM3u8ContentFileExistResult BM3u8ContentFileExistResultMake(NSString *subP,BOOL isAllExist);
CG_INLINE BVideoRecordConfig BVideoRecordConfigMake(BOOL isRecordVideo,BOOL isFrontDevice);
CG_INLINE BButtonConfig BButtonConfigMake(NSString *_Nullable nStr,NSString *_Nullable sStr,UIColor *_Nullable nColor,UIColor *_Nullable sColor,UIFont *_Nullable titleFont,NSString *_Nullable nImgStr,NSString *_Nullable sImgStr,id target,SEL selector);
CG_INLINE BButtonConfig BButtonConfigTMake(NSString *nStr,NSString *sStr,UIColor *nColor,UIColor *sColor,UIFont *titleFont,id target,SEL selector);
CG_INLINE BButtonConfig BButtonConfigIMake(NSString *nImgStr,NSString *sImgStr,id target,SEL selector);
CG_INLINE void BMAIN_THREAD(void(^completeHandler)(void));
CG_INLINE UIWindow *KEY_WINDOW(void);
CG_INLINE UIViewController *ROOT_VC(void);

CG_INLINE UIColor *
BColorMake(CGFloat r,CGFloat g,CGFloat b,CGFloat a){
    return [UIColor colorWithRed:r/255.f green:g/255.f blue:b/255.f alpha:a];
}

CG_INLINE BOOL
IS_NULL_STR(NSString *str){
    NSString *resultStr = @"";
    if(str){
        resultStr = [NSString stringWithFormat:@"%@",str];
    }
    return nil == resultStr || [resultStr isKindOfClass:[NSNull class]] || [@"" isEqualToString:(resultStr)] || [@"<null>" isEqualToString:(resultStr)] || [@"(null)" isEqualToString:(resultStr)];
}

CG_INLINE NSString *
ParseToString(id data){
    return IS_NULL_STR(data) ? @"" : [NSString stringWithFormat:@"%@",data];
}

CG_INLINE BRGB
BRGBMake(CGFloat r,CGFloat g,CGFloat b,CGFloat a){
    BRGB rgb;
    rgb.r = r;
    rgb.g = g;
    rgb.b = b;
    rgb.a = a;
    return rgb;
}

CG_INLINE BVideoCacheResult
BVideoCacheResultMake(NSString *path,BOOL isLocalPath,CGFloat totalDuration){
    BVideoCacheResult result;
    result.path = path;
    result.isLocalPath = isLocalPath;
    result.totalDuration = totalDuration;
    return result;
}

CG_INLINE BM3u8ContentFileExistResult BM3u8ContentFileExistResultMake(NSString *subP,BOOL isAllExist){
    BM3u8ContentFileExistResult result;
    result.subP = subP;
    result.isAllExist = isAllExist;
    return result;
}

CG_INLINE
BVideoRecordConfig BVideoRecordConfigMake(BOOL isRecordVideo,BOOL isFrontDevice){
    BVideoRecordConfig config;
    config.isRecordVideo = isRecordVideo;
    config.isFrontDevice = isFrontDevice;
    return config;
}


CG_INLINE BButtonConfig BButtonConfigMake(NSString *_Nullable nStr,NSString *_Nullable sStr,UIColor *_Nullable nColor,UIColor *_Nullable sColor,UIFont *_Nullable titleFont,NSString *_Nullable nImgStr,NSString *sImgStr,id target,SEL selector){
    BButtonConfig config;
    config.nStr      = nStr;
    config.sStr      = sStr;
    config.nColor    = nColor;
    config.sColor    = sColor;
    config.titleFont = titleFont;
    config.nImgStr   = nImgStr;
    config.sImgStr   = sImgStr;
    config.target    = target;
    config.selector  = selector;
    return config;
}

CG_INLINE BButtonConfig BButtonConfigTMake(NSString *nStr,NSString *sStr,UIColor *nColor,UIColor *sColor,UIFont *titleFont,id target,SEL selector){
    return BButtonConfigMake(nStr, sStr,nColor,sColor,titleFont,@"", @"",target,selector);
}

CG_INLINE BButtonConfig BButtonConfigIMake(NSString *nImgStr,NSString *sImgStr,id target,SEL selector){
    return BButtonConfigMake( @"", @"",nil,nil,nil,nImgStr, sImgStr,target,selector);
}

CG_INLINE void
BMAIN_THREAD(void(^completeHandler)(void)){
    if([NSThread isMainThread]){
        if(completeHandler){
            completeHandler();
        }
    }
    else
    {
        __block typeof(completeHandler) tmpHandler = completeHandler;
        dispatch_async(dispatch_get_main_queue(), ^{
            if(tmpHandler){
                tmpHandler();
            }
        });
    }
}

CG_INLINE UIWindow *
KEY_WINDOW(){
    UIWindow *window = nil;
    if([[UIApplication sharedApplication].delegate respondsToSelector:@selector(window)]){
        window = [UIApplication sharedApplication].delegate.window;
    }
//    if(window){
        return window;
//    }
//    else
//    {
//#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_12_3
//#else
//        if (@available(iOS 13.0,*)) {
//            for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
//                NSArray *winDelegate = [(UIWindowScene *)scene windows];
//                for (UIWindow *w in winDelegate) {
//                    if(w.windowLevel == 0){
//                        window = w;
//                        break;
//                    }
//                }
//                if(window){
//                    break;
//                }
//            }
//        }
//#endif
//        return window;
//    }
}

CG_INLINE UIViewController *
ROOT_VC(){
    UIWindow *window = KEY_WINDOW();
    UIViewController *rootVC = nil;
    if(window){
        rootVC = window.rootViewController;
    }
    return rootVC;
}

@interface BConst : NSObject


/// 跳转url
/// @param urlstr  url链接
+ (void)openUrlWithStr:(NSString *)urlstr;

@end

NS_ASSUME_NONNULL_END
